﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace j1
{

    class Program
    {
        static void Main(string[] args)
        {
            Database.Initialize();

            while (true)
            {
                Console.WriteLine("请选择一个操作:");
                Console.WriteLine("1. 添加学校");
                Console.WriteLine("2. 更新学校");
                Console.WriteLine("3. 删除学校");
                Console.WriteLine("4. 查看所有学校");
                Console.WriteLine("5. 添加班级");
                Console.WriteLine("6. 更新班级");
                Console.WriteLine("7. 删除班级");
                Console.WriteLine("8. 查看所有班级");
                Console.WriteLine("9. 添加学生");
                Console.WriteLine("10. 更新学生");
                Console.WriteLine("11. 删除学生");
                Console.WriteLine("12. 查看所有学生");
                Console.WriteLine("13. 查看日志");
                Console.WriteLine("0. 退出");

                var input = Console.ReadLine();

                if (input == "0")
                {
                    break;
                }

                switch (input)
                {
                    case "1":
                        AddSchool();
                        break;
                    case "2":
                        UpdateSchool();
                        break;
                    case "3":
                        DeleteSchool();
                        break;
                    case "4":
                        ViewAllSchools();
                        break;
                    case "5":
                        AddClass();
                        break;
                    case "6":
                        UpdateClass();
                        break;
                    case "7":
                        DeleteClass();
                        break;
                    case "8":
                        ViewAllClasses();
                        break;
                    case "9":
                        AddStudent();
                        break;
                    case "10":
                        UpdateStudent();
                        break;
                    case "11":
                        DeleteStudent();
                        break;
                    case "12":
                        ViewAllStudents();
                        break;
                    case "13":
                        LogDataAccess.ShowLogs();
                        break;
                    default:
                        Console.WriteLine("无效的选择，请重试。");
                        break;
                }
            }
        }

        private static void AddSchool()
        {
            Console.Write("输入学校名称: ");
            string name = Console.ReadLine();
            School school = new School { Name = name };
            SchoolDataAccess.AddSchool(school);
            Console.WriteLine("学校添加成功。");
        }

        private static void UpdateSchool()
        {
            Console.Write("输入学校ID: ");
            int id = int.Parse(Console.ReadLine());
            School school = SchoolDataAccess.GetSchool(id);
            if (school != null)
            {
                Console.Write("输入新的学校名称: ");
                school.Name = Console.ReadLine();
                SchoolDataAccess.UpdateSchool(school);
                Console.WriteLine("学校更新成功。");
            }
            else
            {
                Console.WriteLine("学校未找到。");
            }
        }

        private static void DeleteSchool()
        {
            Console.Write("输入学校ID: ");
            int id = int.Parse(Console.ReadLine());
            SchoolDataAccess.DeleteSchool(id);
            Console.WriteLine("学校删除成功。");
        }

        private static void ViewAllSchools()
        {
            List<School> schools = SchoolDataAccess.GetAllSchools();
            foreach (var sch in schools)
            {
                Console.WriteLine($"学校ID: {sch.Id}, 名称: {sch.Name}");
            }
        }

        private static void AddClass()
        {
            Console.Write("输入班级名称: ");
            string name = Console.ReadLine();
            Console.Write("输入学校ID: ");
            int schoolId = int.Parse(Console.ReadLine());
            Class cls = new Class { Name = name, SchoolId = schoolId };
            ClassDataAccess.AddClass(cls);
            Console.WriteLine("班级添加成功。");
        }

        private static void UpdateClass()
        {
            Console.Write("输入班级ID: ");
            int id = int.Parse(Console.ReadLine());
            Class cls = ClassDataAccess.GetClass(id);
            if (cls != null)
            {
                Console.Write("输入新的班级名称: ");
                cls.Name = Console.ReadLine();
                Console.Write("输入新的学校ID: ");
                cls.SchoolId = int.Parse(Console.ReadLine());
                ClassDataAccess.UpdateClass(cls);
                Console.WriteLine("班级更新成功。");
            }
            else
            {
                Console.WriteLine("班级未找到。");
            }
        }

        private static void DeleteClass()
        {
            Console.Write("输入班级ID: ");
            int id = int.Parse(Console.ReadLine());
            ClassDataAccess.DeleteClass(id);
            Console.WriteLine("班级删除成功。");
        }

        private static void ViewAllClasses()
        {
            List<Class> classes = ClassDataAccess.GetAllClasses();
            foreach (var cls in classes)
            {
                Console.WriteLine($"班级ID: {cls.Id}, 名称: {cls.Name}, 学校ID: {cls.SchoolId}");
            }
        }

        private static void AddStudent()
        {
            Console.Write("输入学生姓名: ");
            string name = Console.ReadLine();
            Console.Write("输入班级ID: ");
            int classId = int.Parse(Console.ReadLine());
            Student student = new Student { Name = name, ClassId = classId };
            StudentDataAccess.AddStudent(student);
            Console.WriteLine("学生添加成功。");
        }

        private static void UpdateStudent()
        {
            Console.Write("输入学生ID: ");
            int id = int.Parse(Console.ReadLine());
            Student student = StudentDataAccess.GetStudent(id);
            if (student != null)
            {
                Console.Write("输入新的学生姓名: ");
                student.Name = Console.ReadLine();
                Console.Write("输入新的班级ID: ");
                student.ClassId = int.Parse(Console.ReadLine());
                StudentDataAccess.UpdateStudent(student);
                Console.WriteLine("学生更新成功。");
            }
            else
            {
                Console.WriteLine("学生未找到。");
            }
        }

        private static void DeleteStudent()
        {
            Console.Write("输入学生ID: ");
            int id = int.Parse(Console.ReadLine());
            StudentDataAccess.DeleteStudent(id);
            Console.WriteLine("学生删除成功。");
        }

        private static void ViewAllStudents()
        {
            List<Student> students = StudentDataAccess.GetAllStudents();
            foreach (var stu in students)
            {
                Console.WriteLine($"学生ID: {stu.Id}, 姓名: {stu.Name}, 班级ID: {stu.ClassId}");
            }
        }
    }


}
